/*
 * check.cpp - check that constraints are met for each containing row, column, and square
 * Yet Another Sudoku Solver 
 * CSCE 411
 * by Michael Atkinson
 */

#include "common.h"
#include <cmath>

int check(vector<vector<char> > &puzzle, unsigned int r, unsigned int c)
{
	unsigned int pzsize = puzzle.size();

	// check column constraints
	for(unsigned int i = 0; i < pzsize; i++)
	{
		if(r == i)
			continue;
		if(puzzle[i][c] == puzzle[r][c])
			return 0;
	}

	// check row constraints
	for(unsigned int i = 0; i < pzsize; i++)
	{
		if(c == i)
			continue;
		if(puzzle[r][i] == puzzle[r][c])
			return 0;
	}

	// degree of puzzle == size of grids
	int d = (int)(sqrt((double)pzsize));
	
	// check grid constraints
	for(unsigned int i = d * (r / d); i < d * (r / d) + d; i++)
		for(unsigned int j = d * (c / d); j < d * (c / d) + d; j++)
		{
			if(r == i && c == j)
				continue;
			if(puzzle[i][j] == puzzle[r][c])
				return 0;
		}

	return 1; // all constraints met, value preserves Sudoku properties
}